package com.csproj.system.repository;

import com.csproj.system.domain.AuthorityInfo;

import com.csproj.system.repository.custom.AuthorityInfoRepositoryCustom;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface AuthorityInfoRepository extends JpaRepository<AuthorityInfo,String> , AuthorityInfoRepositoryCustom {

    /**
     * 根据id查询权限
     * @return
     */
    @Query(" from AuthorityInfo ai where ai.deletedFlag = 0 and ai.id = ?1 ")
    AuthorityInfo findone(String id);
    /**
     * 查询未删除的权限信息
     * @return
     */
    @Query(" from AuthorityInfo ai where ai.deletedFlag = 0 ")
    List<AuthorityInfo> findAll();

    /**
     * 删除权限信息
     * @param id
     */
    @Modifying
    @Query(" update AuthorityInfo ai SET ai.deletedFlag = 1 where ai.id = ?1")
    void delete(String id);

    /**
     * 根据 code 查询当前权限的所有父级权限
     * @param code
     * @return
     */
//    @Query("SELECT ai FROM AuthorityInfo ai where ai.isDelete = 0 and ai.code = ?1")
//    List<AuthorityInfo> findByCode (String code);

    /**
     * 根据  code 查询当前权限的所有子权限
     * @param code
     * @return
     */
    @Query("SELECT ai FROM AuthorityInfo ai where ai.deletedFlag = 0 and ai.parentCode like %?1%")
    List<AuthorityInfo> findByCode(String code);

    /**
     * 根据用户的账号 获取用户角色下的权限
     *  先获取用户的多个角色，再获取角色的多个权限，获取权限时要去重
     * @param userAccount       用户的账号
     * @return
     *//*
    @Query("select DISTINCT ai from AuthorityInfo ai left join ai.roleInfos ri left join ri.userInfos ui where ai.isDelete = 0 and ri.isDelete = 0 and ui.account = ?1")
    List<AuthorityInfo> findByUserinfoAccount (String userAccount);*/

    /**
     * 根据用户id 获取用户角色下的权限
     * @param userInfoId
     * @return
     */
    @Query("select DISTINCT ai from AuthorityInfo ai left join ai.roleInfos ri left join ri.userInfos ui where ai.deletedFlag = 0 and ri.deletedFlag = 0 and ui.id = ?1")
    List<AuthorityInfo> findByUserInfoId(String userInfoId);

    /**
     * 根据用户id，与parentCode 查找权限
     */
    @Query(" select DISTINCT ai from AuthorityInfo ai left join ai.roleInfos ri left join ri.userInfos ui where ai.deletedFlag = 0 and ri.deletedFlag = 0 and ui.id = ?1 and ai.parentCode = ?2 ")
    List<AuthorityInfo> findByUserInfoIdAndParentCode(String userInfoId, String parentCode);

    /**
     * 根据用户id与权限id
     */
    /**
     * 根据角色id获取角色下的权限
     * @param roleInfoId        角色id
     * @return
     */
    @Query("select ai from AuthorityInfo ai left join ai.roleInfos ri where ai.deletedFlag = 0 and ri.deletedFlag = 0 and ri.id = ?1")
    List<AuthorityInfo> findByRoleInfoId(String roleInfoId);


    /**
     * 权限标识查重
     * @param permission
     * @param id
     * @return
     */
    @Query(" from AuthorityInfo where deletedFlag = 0 and permission = ?1 and id <> ?2 ")
    List<AuthorityInfo> findByPermissionAndId(String permission, String id);



    /**
     * 根据标识查询
     * @param permission
     * @return
     */
    @Query(" from AuthorityInfo where deletedFlag = 0 and permission = ?1 ")
    List<AuthorityInfo> findByPermission(String permission);

    /**
     * 批量删除
     */
    @Modifying
    @Query(" update AuthorityInfo ai SET ai.deletedFlag = 1 where ai.id in (?1) ")
    void deleteBatch(String[] ids);

    /**
     * 根据创建时间倒叙排序
     * @return  code
     */
    @Query(value = "select ai.code from sys_authority_info ai order by  creation_time desc limit 1 ",nativeQuery = true)
    String findLastOneOrderByCreateTime();
}
